﻿
namespace OneWorks.Application
{
    using System;
    using Interface;
    using Entity;

    using Repositories.Interface;

    using Repositories.EntityFramework;
    using Utils.Specifications;
    using Metadata.Entity;
    using Metadata.Enum;

    public class ImageService : BaseService, IImageService
    {

        private readonly IImageRepository _imageRepository;

        private readonly IUserRepository _userRepository;

        static ImageService()
        {
            AutoMapper.Mapper.CreateMap<DataBase.Entity.Image, Image>();
            AutoMapper.Mapper.CreateMap<Image, DataBase.Entity.Image>();
        }

        public ImageService()
        {
            _imageRepository = new ImageRepository(RepositoryContext);
            _userRepository = new UserRepository(RepositoryContext);
        }

        public RunResult<Image> Get(string url)
        {
            RunResult<Image> runResult = new RunResult<Image>();
            try
            {
                DataBase.Entity.Image image =
                    _imageRepository.Get(Specification<DataBase.Entity.Image>.Eval(i => i.Src == url));
                runResult.Status = ResultStatus.Success;
                runResult.Result = AutoMapper.Mapper.Map<DataBase.Entity.Image, Image>(image);
            }
            catch (Exception exception)
            {
                runResult.Exception = exception;
                runResult.Message = string.Format("添加图片异常,参数{0}", url);
                runResult.Status = ResultStatus.Abnormal;
            }
            return runResult;
        }

        public RunResult<Image> Add(Image image)
        {
            RunResult<Image> runResult = new RunResult<Image>();
            try
            {
                if (_imageRepository.Exists(Specification<DataBase.Entity.Image>.Eval(i => i.Src == image.Src || i.Path == image.Path)))
                {
                    return new RunResult<Image>
                    {
                        Status = ResultStatus.Failure,
                        Message = "图片已经存在"
                    };
                }
                if (!_userRepository.Exists(Specification<DataBase.Entity.User>.Eval(i => i.ID == image.UserID)))
                {
                    return new RunResult<Image>
                    {
                        Status = ResultStatus.Failure,
                        Message = "添加图片操作该用户不存在"
                    };
                }
                DataBase.Entity.Image dbImage = AutoMapper.Mapper.Map<Image, DataBase.Entity.Image>(image);
                dbImage.ID = Guid.NewGuid();
                _imageRepository.Add(dbImage);
                int r = RepositoryContext.Commit();
                if (r > 0)
                {
                    runResult.Status = ResultStatus.Success;
                    runResult.Message = "图片添加成功";
                    image.ID = dbImage.ID;
                    runResult.Result = image;
                }
                else
                {
                    runResult.Status = ResultStatus.Failure;
                    runResult.Message = "图片添加数据库操作失败";
                }
            }
            catch (Exception exception)
            {
                runResult.Status = ResultStatus.Abnormal;
                runResult.Message = "图片添加出现异常";
                runResult.Exception = exception;
            }
            return runResult;
        }

        public RunResult<bool> Exists(string url)
        {
            RunResult<bool> runResult = new RunResult<bool>();
            try
            {
                runResult.Status = ResultStatus.Success;
                runResult.Result = _imageRepository.Exists(Specification<DataBase.Entity.Image>.Eval(i => i.Src == url));
            }
            catch (Exception exception)
            {
                Log.Error(string.Format("验证图片是否存在异常,参数{0}", url), exception);
                runResult.Exception = exception;
                runResult.Message = string.Format("验证图片是否存在异常,参数{0}", url);
                runResult.Status = ResultStatus.Abnormal;
            }
            return runResult;
        }

        public RunResult<PagedResult<Image>> Find(Image image, FindPage page)
        {
            RunResult<PagedResult<Image>> result = new RunResult<PagedResult<Image>>();
            try
            {
                result.Result = new PagedResult<Image>();
                ISpecification<DataBase.Entity.Image> specification = new AnySpecification<DataBase.Entity.Image>();
                if (image.ID != Guid.Empty)
                    specification = specification.And(Specification<DataBase.Entity.Image>.Eval(i => i.ID == image.ID));
                if (!string.IsNullOrEmpty(image.ImgType))
                    specification = specification.And(Specification<DataBase.Entity.Image>.Eval(i => i.ImgType == image.ImgType));
                if (image.ImgUse != ImageUse.All)
                    specification = specification.And(Specification<DataBase.Entity.Image>.Eval(i => i.ImgUse == image.ImgUse));
                if (!string.IsNullOrEmpty(image.Path))
                    specification = specification.And(Specification<DataBase.Entity.Image>.Eval(i => i.Path == image.Path));
                if (!string.IsNullOrEmpty(image.Src))
                    specification = specification.And(Specification<DataBase.Entity.Image>.Eval(i => i.Src == image.Src));
                if (image.UserID != Guid.Empty)
                    specification = specification.And(Specification<DataBase.Entity.Image>.Eval(i => i.UserID == image.UserID));
                PagedResult<DataBase.Entity.Image> pagedResult = _imageRepository.Find(specification, page);
                var list = pagedResult.GetEnumerator();
                while (list.MoveNext())
                {
                    result.Result.Data.Add(AutoMapper.Mapper.Map<DataBase.Entity.Image, Image>(list.Current));
                }
                result.Result.TotalPages = pagedResult.TotalPages;
                result.Result.TotalRecords = pagedResult.TotalRecords;
                result.Status = ResultStatus.Success;
                return result;
            }
            catch (Exception exception)
            {
                Log.Error("图片列表查询出现异常", exception);
                result.Exception = exception;
                result.Status = ResultStatus.Abnormal;
                result.Message = "图片列表查询出现异常";
                result.Result = new PagedResult<Image>();
            }
            return result;
        }
    }
}
